/**
 *
 * @author spider
 */
package br.unb.plugins.geral.view;

import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import br.unb.plugins.geral.validation.ValidateException;
import br.unb.plugins.geral.Action.XmlFrameAction;
import br.unb.plugins.geral.Action.DefaultOKAction;
import br.unb.plugins.geral.model.XmlComponentModel;
import br.unb.plugins.geral.validation.Validation;
import br.unb.utils.Console;
import br.unb.utils.Resource;

public class XmlComponent {

    Component component = null;
    XmlComponentModel data = null;
    boolean parameter = false;

    public Component getComponent() {
        return component;
    }

    public boolean isParameter() {
        return parameter;
    }

    public void setParameter(boolean parameter) {
        this.parameter = parameter;
    }

    public XmlComponentModel getData() {
        return data;
    }

    public Component createXmlComponent(XmlComponentModel c, XmlFrame parent) {
        data = c;
        try {
            if (c.getComponentType().equalsIgnoreCase(XmlComponentModel.FIELD)) {
                int size = c.getXSizeValue();
                component = new JTextField(size);
                setParameter(true);
            } else if (c.getComponentType().equalsIgnoreCase(XmlComponentModel.COMBO)) {
                JComboBox combo = null;
                if (c.getValues() != null) {
                    combo = new JComboBox(c.getValues().toArray());

                } else if (c.getXmlComboPopulator() != null) {
                    combo = new XmlComboBox(c.getXmlComboPopulator());
                }
                if (c.getAction() != null) {
                    //TODO write a new xml file which uses the <action> label
                    XmlFrameAction act = (XmlFrameAction) (Class.forName(c.getAction()).newInstance());
                    act.setHomeFrame(parent);
                    combo.addActionListener(act);
                }
                component = combo;
                setParameter(true);
            } else if (c.getComponentType().equalsIgnoreCase(XmlComponentModel.BUTTON)) {
                JButton button = new JButton(c.getName());
                if (c.getAction() != null) {
                    //TODO write a new xml file which uses the <action> label
                    XmlFrameAction act = (XmlFrameAction) (Class.forName(c.getAction()).newInstance());
                    act.setHomeFrame(parent);
                    button.addActionListener(act);
                } else {
                    DefaultOKAction act = new DefaultOKAction();
                    act.setHomeFrame(parent);
                    button.addActionListener(act);
                }
                component = button;
            } else if (c.getComponentType().equalsIgnoreCase(XmlComponentModel.BOX)) {
                component = Box.createRigidArea(new Dimension(c.getXSizeValue(), c.getYSizeValue()));
            } else if (c.getComponentType().equalsIgnoreCase(XmlComponentModel.LABEL)) {
                component = new JLabel(c.getText());
            }

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return component;
    }

    public void validate() throws ValidateException {
        String problem;
        String allProblems = "";
        if (isParameter() || component instanceof JTextField) {
            ArrayList validations = (ArrayList) data.getValidations();
            if (validations != null) {
                Iterator it = validations.iterator();
                while (it.hasNext()) {
                    String cl = (String) it.next();
                    try {
                        Validation validation = (Validation) (Class.forName(cl).newInstance());
                        problem = validation.validate(((JTextField) component).getText());

                        if (problem != null) {
                            allProblems += "\t" + problem + "\n";
                        }

                    } catch (InstantiationException e) {
                        Console.printError(e.getLocalizedMessage());
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        Console.printError(e.getLocalizedMessage());
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        Console.printError(e.getLocalizedMessage());
                        e.printStackTrace();
                    }
                }
                if (!allProblems.isEmpty()) {
                    ((JTextField) component).setText("");
                    throw new ValidateException(
                            Resource.getString("FIELD")
                            + data.getName()
                            + "\n"
                            + allProblems);
                }
            }
        }
    }
}

